package net.zoneland.knowledge.service.impl;

import net.zoneland.knowledge.constant.DocumentVerificationConstants;
import net.zoneland.knowledge.controller.bean.TermInfo;
import net.zoneland.knowledge.dao.TermInfoMapper;
import net.zoneland.knowledge.exceptions.DocumentVerificationException;
import net.zoneland.knowledge.service.TermsInfoManagementService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * 名词词典服务接口.
 */
@Service
public class TermsInfoManagementServiceImpl implements TermsInfoManagementService {

    /**
     * 名词信息数据接口.
     */
    @Resource
    private TermInfoMapper termInfoMapper;


    /**
     * 从数据库模糊查询名词信息.
     *
     * @param termInfo 名词信息对象.
     * @return 名词词典信息列表.
     */
    @Override
    public List<TermInfo> queryTermsLike(final TermInfo termInfo) {
        return termInfoMapper.search(termInfo);
    }

    /**
     * 更新名词.
     *
     * @param termInfo 名词信息对象.
     */
    @Override
    public void updateTermById(final TermInfo termInfo) {
        termInfoMapper.updateTerm(termInfo);
    }

    /**
     * 存储名词词典信息.
     * @param termInfo 名词信息.
     * @throws DocumentVerificationException 文档检验异常信息-新增名词异常
     */
    @Override
    public void addTerm(final TermInfo termInfo) throws DocumentVerificationException {
        //名词要唯一，重复的时候抛出异常
        if(termInfoMapper.isTermExist(termInfo.getTermName())){
            throw new DocumentVerificationException(DocumentVerificationConstants.TERM_EXIST_ERROR
                    + ",term name is [" + termInfo.getTermName() + "]");
        }
        termInfoMapper.insert(termInfo);
    }

    /**
     * 通过主键id删除名词.
     *
     * @param ids 名词词典表主键id.
     */
    @Override
    public void deleteByPrimaryKey(final String[] ids) {
        termInfoMapper.deleteBatch(ids);
    }
}
